//
//  WalletView.m
//  dhfsAPP
//
//  Created by Devil on 2017/7/31.
//  Copyright © 2017年 Devil. All rights reserved.
//

#import "WalletView.h"
@interface WalletView()

//顶栏视图
@property(strong,nonatomic)HeaderView *headerView;

//充值按钮,确认支付按钮,关闭按钮
@property(strong,nonatomic)UIButton *rechargeBtn,*currentBtn,*shutDownBtn,*findMoney;

//余额视图、充值视图
@property(strong,nonatomic)UIView *balanceView,*rechargeView;

//充值记录
@property(nonatomic,strong)UITableView *tableView;

//充值金额
@property(nonatomic,strong)NSArray *moneyArray;

@end
@implementation WalletView

#pragma mark - 懒加载
-(HeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT(128)) Title:@"我的钱包"];
        
        _headerView.headerViewDelegate = self;
        
        [_headerView addSubview:self.rechargeBtn];
        
    }
    
    return _headerView;
}

-(UIButton *)rechargeBtn{
    if (!_rechargeBtn) {
        _rechargeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _rechargeBtn.frame = CGRectMake(WIDTH(649), 0, 0, 0);
        
        [_rechargeBtn setTitle:@"充值" forState:UIControlStateNormal];
        
        [_rechargeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        _rechargeBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(32)];
        
        [_rechargeBtn sizeToFit];
        
        _rechargeBtn.center = CGPointMake(_rechargeBtn.center.x, self.headerView.title.center.y);
        
        _rechargeBtn.tag = WalletViewButtonTypeRecharge;
        
        [_rechargeBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _rechargeBtn;
}

-(UIView *)balanceView{
    if (!_balanceView) {
        _balanceView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT(128), SCREEN_WIDTH, HEIGHT(280))];
        
        _balanceView.backgroundColor = [UIColor whiteColor];
        
        UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"我的钱包-大"]];
        
        imgV.frame = CGRectMake(0, HEIGHT(30), WIDTH(106), HEIGHT(110));
        
        imgV.center = CGPointMake(SCREEN_WIDTH / 2.0, imgV.center.y);
        
        [_balanceView addSubview:imgV];
        
        UILabel *moneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, HEIGHT(161), 0, 0)];
        
        moneyLabel.text = @"零钱";
        
        moneyLabel.textColor = RGB(51, 51, 51);
        
        moneyLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(28)];
        
        [moneyLabel sizeToFit];
        
        moneyLabel.center = CGPointMake(SCREEN_WIDTH / 2.0, moneyLabel.center.y);
        
        [_balanceView addSubview:moneyLabel];
        
        [_balanceView addSubview:self.balanceMoney];
        [_balanceView addSubview:self.findMoney];
    }
    
    return _balanceView;
}

-(UILabel *)balanceMoney{
    if (!_balanceMoney) {
        _balanceMoney = [[UILabel alloc] initWithFrame:CGRectMake(0, HEIGHT(201), 0, 0)];
        
        _balanceMoney.text = @"获取中";
        
        _balanceMoney.textColor = RGB(0, 105, 193);
        
        _balanceMoney.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(26)];
        
        [_balanceMoney sizeToFit];
        
        _balanceMoney.center = CGPointMake(SCREEN_WIDTH / 2.0, _balanceMoney.center.y);
    }
    
    return _balanceMoney;
}

-(UIButton *)findMoney{
    if (!_findMoney) {
        _findMoney = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _findMoney.frame = CGRectMake(0, HEIGHT(220), 0, 0);
        
        [_findMoney setTitle:@"汉点不对？点击找回" forState:UIControlStateNormal];
        [_findMoney setTitleColor:RGB(235,24,116) forState:UIControlStateNormal];
        _findMoney.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(26)];
        
        [_findMoney sizeToFit];
        
        _findMoney.center = CGPointMake(SCREEN_WIDTH / 2.0, HEIGHT(36));
        
        _findMoney.tag = WalletViewButtonTypeFind;
        
        [_findMoney addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _findMoney;
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT(423), SCREEN_WIDTH, SCREEN_HEIGHT - HEIGHT(423))];
        
        _tableView.delegate = self;
        
        _tableView.dataSource = self;
        
        _tableView.backgroundColor = [UIColor whiteColor];
        
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    
    return _tableView;
}

-(UIView *)rechargeView{
    if (!_rechargeView) {
        _rechargeView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT(128), SCREEN_WIDTH, SCREEN_HEIGHT - HEIGHT(128))];
        
        _rechargeView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        
        UIView *iView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH(75), HEIGHT(200), WIDTH(601), HEIGHT(772))];
        
        iView.backgroundColor = [UIColor whiteColor];
        
        [iView addSubview:self.shutDownBtn];
        
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, HEIGHT(40), 0, 0)];
        
        title.text = @"汉点充值";
        title.textColor = RGB(51, 51, 51);
        title.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(42)];
        
        [title sizeToFit];
        
        title.center = CGPointMake(WIDTH(601) / 2.0, self.shutDownBtn.center.y);
        
        [iView addSubview:title];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH(30), HEIGHT(116), 0, 0)];
        
        label.text = @"充值金额";
        label.textColor = RGB(51, 51, 51);
        label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(32)];
        
        [label sizeToFit];
        
        [iView addSubview:label];
        
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                
                btn.frame = CGRectMake(WIDTH(30) + j * WIDTH(193), HEIGHT(171) + i * HEIGHT(134), WIDTH(150), HEIGHT(100));
                
                NSMutableAttributedString *atrString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@\n%@汉点",self.moneyArray[i][j],self.moneyArray[i][j]]];
                
                [atrString addAttribute:NSForegroundColorAttributeName value:RGB(0, 105, 193) range:NSMakeRange(0, atrString.length)];
                
                [atrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(20)] range:NSMakeRange(0, atrString.length)];
                
                [atrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(32)] range:NSMakeRange(1, [NSString stringWithFormat:@"%@",self.moneyArray[i][j]].length)];
                
                NSMutableAttributedString *atrString2 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@\n%@汉点",self.moneyArray[i][j],self.moneyArray[i][j]]];
                
                [atrString2 addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, atrString.length)];
                
                [atrString2 addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(20)] range:NSMakeRange(0, atrString.length)];
                
                [atrString2 addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(32)] range:NSMakeRange(1, [NSString stringWithFormat:@"%@",self.moneyArray[i][j]].length)];
                
                [btn setAttributedTitle:atrString forState:UIControlStateNormal];
                [btn setAttributedTitle:atrString2 forState:UIControlStateSelected];
                
                btn.titleLabel.numberOfLines = 2;
                
                btn.layer.masksToBounds = YES;
                btn.layer.cornerRadius = 5.0;
                btn.layer.borderColor = RGB(0, 105, 193).CGColor;
                btn.layer.borderWidth = WIDTH(1);
                
                btn.tag = 1000 + j + i * 3;
                
                if (i == 0 && j == 0)
                {
                    btn.selected = YES;
                    self.lastBtn = btn;
                }
                else
                {
                    btn.selected = NO;
                }
                
                [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
                
                if (btn.selected == NO)
                {
                    btn.backgroundColor = [UIColor whiteColor];
                }
                else
                {
                    btn.backgroundColor = RGB(0, 105, 193);
                }
                
                [iView addSubview:btn];
            }
        }
        
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH(30), HEIGHT(459), 0, 0)];
        
        label2.text = @"支付金额";
        label2.textColor = RGB(51, 51, 51);
        label2.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(32)];
        
        [label2 sizeToFit];
        
        [iView addSubview:label2];
        
        [iView addSubview:self.payMoney];
        UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"温馨提示"]];
        
        imgV.frame = CGRectMake(WIDTH(31), HEIGHT(536), WIDTH(40), HEIGHT(40));
        
        [iView addSubview:imgV];
        
        UILabel *hint = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH(83), HEIGHT(533), 0, 0)];
        
        hint.text = @"温馨提示：\n1.汉点兑换规则为1元=1汉点。\n2.游客充值的汉点只能在当前设备上使用";
        hint.numberOfLines = 3;
        hint.textColor = RGB(235, 24, 116);
        hint.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(26)];
        
        [hint sizeToFit];
        
        hint.center = CGPointMake(hint.center.x, imgV.center.y);
        
        [iView addSubview:hint];
        
        [iView addSubview:self.currentBtn];
        
        [_rechargeView addSubview:iView];
        _rechargeView.hidden = YES;
        
    }
    
    return _rechargeView;
}

-(UILabel *)payMoney{
    if (!_payMoney) {
        _payMoney = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH(190), HEIGHT(457), 0, 0)];
        
        NSMutableAttributedString *atrString = [[NSMutableAttributedString alloc] initWithString:@"￥6"];
        
        [atrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(20)] range:NSMakeRange(0, 1)];
        [atrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(32)] range:NSMakeRange(1, 1)];
        
        [atrString addAttribute:NSForegroundColorAttributeName value:RGB(235,24,116) range:NSMakeRange(0, 2)];
        
        _payMoney.attributedText = atrString;
        
        [_payMoney sizeToFit];
    }
    
    return _payMoney;
}

-(UIButton *)currentBtn{
    if (!_currentBtn) {
        _currentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _currentBtn.frame = CGRectMake(WIDTH(141), HEIGHT(617), WIDTH(321), HEIGHT(71));
        
        [_currentBtn setTitle:@"确定支付" forState:UIControlStateNormal];
        [_currentBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _currentBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(32)];
        
        _currentBtn.backgroundColor = RGB(0, 105, 193);
        
        _currentBtn.tag = WalletViewButtonTypeCurrent;
        
        [_currentBtn addTarget:self action:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
        
        _currentBtn.layer.masksToBounds = YES;
        _currentBtn.layer.cornerRadius = 10.0;
        
    }
    
    return _currentBtn;
}

-(UIButton *)shutDownBtn{
    if (!_shutDownBtn) {
        _shutDownBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _shutDownBtn.frame = CGRectMake(WIDTH(535), HEIGHT(46), WIDTH(29), HEIGHT(29));
        
        [_shutDownBtn setImage:[UIImage imageNamed:@"叉"] forState:UIControlStateNormal];
        
        _shutDownBtn.tag = WalletViewButtonTypeShut;
        
        [_shutDownBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _shutDownBtn;
}

-(NSArray *)moneyArray{
    if (!_moneyArray) {
        _moneyArray = [NSArray arrayWithObjects:[NSArray arrayWithObjects:@"6",@"12",@"18",nil],[NSArray arrayWithObjects:@"25",@"50",@"98",nil] ,nil];
    }
    
    return _moneyArray;
}

#pragma mark - 构造函数
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if (self) {
        self.backgroundColor = RGB(241, 242, 242);
        
        [self initUI];
    }
    
    return self;
    
}

#pragma mark - TableViewDelegate&TableViewDataSource实现
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _payHistoryDic.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return HEIGHT(102);
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return HEIGHT(72);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT(72))];
    
    headerView.backgroundColor = [UIColor whiteColor];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH(39), 0, 0, 0)];
    
    title.text = @"充值记录";
    title.textColor = RGB(0, 105, 193);
    title.font = [UIFont systemFontOfSize:HEIGHT(32)];
    
    [title sizeToFit];
    title.center = CGPointMake(title.center.x, headerView.center.y);
    
    [headerView addSubview:title];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT(70), SCREEN_WIDTH, HEIGHT(2))];
    
    lineView.backgroundColor = RGB(221, 221,221);
    
    [headerView addSubview:lineView];
    
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return HEIGHT(72);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *iView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT(72))];
    
    [iView addSubview:self.findMoney];
    
    iView.backgroundColor = [UIColor whiteColor];
    
    return iView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *str = @"cell";
    UITableViewCell *cell = nil;
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    else
    {
        while ([cell.contentView.subviews lastObject] != nil)
        {
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    
    cell.selectionStyle = NO;
    
    cell.backgroundColor = RGB(221,221, 221);
    
    NSLog(@"%@",self.payHistoryDic);
    NSLog(@"%ld",(long)indexPath.row);
    NSArray *array = (NSArray *)self.payHistoryDic;
    NSLog(@"%@",array[0]);
//    RechargeCellView *cellView = [[RechargeCellView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT(101)) PayMoney:array[self.payHistoryDic.count - indexPath.row - 1][@"recharge"] PayTime:array[self.payHistoryDic.count - indexPath.row - 1][@"time"]];
    RechargeCellView *cellView = [[RechargeCellView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT(101)) PayMoney:array[indexPath.row][@"recharge"] PayTime:array[indexPath.row][@"time"]];
    
    [cell addSubview:cellView];
    
    return cell;
}

#pragma mark - HeaderViewDelegate实现
-(void)clickHeaderViewBtn{
    if ([self.walletViewDelegate respondsToSelector:@selector(backVC)]) {
        [self.walletViewDelegate backVC];
    }
}

#pragma mark - 其他函数
-(void)initUI{
    [self addSubview:self.headerView];
    
    [self addSubview:self.balanceView];
    
    [self addSubview:self.tableView];
    
    [self addSubview:self.rechargeView];
}

-(void)click:(UIButton *)btn{
    if (btn.tag >= 1000 )
    {
        if ([self.walletViewDelegate respondsToSelector:@selector(chooseMoney:)])
        {
            [self.walletViewDelegate chooseMoney:btn];
        }
    }
    else
    {
        if ([self.walletViewDelegate respondsToSelector:@selector(clickWalletViewBtn:)])
        {
            [self.walletViewDelegate clickWalletViewBtn:btn];
        }
    }
}

-(void)changePayMoney:(NSInteger)tag{
    NSInteger i = 0;
    NSInteger j = 0;
    if (tag > 2)
    {
        i = 1;
        j = tag - 3;
    }
    else
    {
        i = 0;
        j = tag;
    }
    
    NSMutableAttributedString *atrString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@",self.moneyArray[i][j]]];
    
    [atrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(20)] range:NSMakeRange(0, 1)];
    [atrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(32)] range:NSMakeRange(1, [NSString stringWithFormat:@"%@",self.moneyArray[i][j]].length)];
    
    [atrString addAttribute:NSForegroundColorAttributeName value:RGB(235,24,116) range:NSMakeRange(0, atrString.length)];
    
    _payMoney.attributedText = atrString;
    
    [_payMoney sizeToFit];
}

-(void)showRecharView:(BOOL)isShow{
    self.rechargeView.hidden = isShow;
}

-(void)reloadTableView{
    [self.tableView reloadData];
}
@end
